package cn.gok.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author xjk
 * @date 2022/3/10 11:19
 * 自定义工具类，将request 中map里的数据取出，封装到一个对象中并返回
 */
public class MapObjectUtil {

    /*
            将request中的数据取出，转成任意我们需要的对象
            request   请求对象
            aclass   要转换对象的类型
        * */
    public static <T> List<T> getObject(List<Map<String, Object>> maps, Class<T> aclass) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException, ParseException {
        List<T> list = new ArrayList<>();
        //判断list集合里有多少map集合
        int size = maps.size();
        T[] o = (T[]) new Object[size];
        for (int i = 0; i < size; i++) {
            o[i] = aclass.getDeclaredConstructor().newInstance();
        }

        Field[] fields = aclass.getDeclaredFields();

        Map<String, Object> map = maps.get(0);

        for (String key : map.keySet()) {
            //如果key对应的属性存在，我们才对对应的属性进行赋值
            if (fieldIsExit(fields, key)) {
                //获取key对应的属性
                Field field = aclass.getDeclaredField(key);
                //打破属性的私有封装
                field.setAccessible(true);
                Class<?> type = field.getType();
                if (maps.get(0).get(key) == null || maps.get(0).get(key) == "") {
                    for (int i = 0; i < size; i++) {
                        field.set(o[i], null);
                    }
                }
                //是否为字符串
                else if (type.equals(String.class)){
                    if (maps.get(0).get(key) != null || maps.get(0).get(key) != "") {
                        for (int i = 0; i < size; i++) {
                            field.set(o[i], String.valueOf(maps.get(i).get(key)) );
                        }
                    } }else if (type.equals(Date.class)) {
                        for (int i = 0; i < size; i++) {
                            Date date = new SimpleDateFormat("yyyy-MM--dd HH:mm:ss").parse((String) maps.get(i).get(key));
                            field.set(o[i], date);
                        }
                    } else if (type.equals(Boolean.class) || type.equals(boolean.class)) {
                        for (int i = 0; i < size; i++) {
                            field.set(o[i], (Boolean) maps.get(i).get(key));
                        }
                    } else if (type.equals(Integer.class) || type.equals(int.class)) {
                        for (int i = 0; i < size; i++) {
                            field.set(o[i],  ((Number)maps.get(i).get(key)).intValue());
                        }
                    } else if (type.equals(Long.class) || type.equals(long.class)) {
                        for (int i = 0; i < size; i++) {
                            field.set(o[i], (Long) maps.get(i).get(key));
                        }
                    } else if (type.equals(Short.class) || type.equals(short.class)) {
                        for (int i = 0; i < size; i++) {
                            field.set(o[i], (Short) maps.get(i).get(key));
                        }
                    } else if (type.equals(Byte.class) || type.equals(byte.class)) {
                        for (int i = 0; i < size; i++) {
                            field.set(o[i], (Byte) maps.get(i).get(key));
                        }
                    } else if (type.equals(Double.class) || type.equals(double.class)) {
                        for (int i = 0; i < size; i++) {
                            field.set(o[i], (Double) maps.get(i).get(key));
                        }
                    } else if (type.equals(Float.class) || type.equals(float.class)) {
                        for (int i = 0; i < size; i++) {
                            field.set(o[i], (Float) maps.get(i).get(key));
                        }
                    }
                }


        }
        for (int i = 0; i < size; i++) {
            list.add(o[i]);
        }
        return list;
    }


    //判断在相应的对象 是否有对应的属性存在
    private static boolean fieldIsExit(Field[] fields, String key) {
        for (Field field : fields) {
            if (field.getName().equals(key)) {
                return true;
            }
        }
        return false;
    }

}
